﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DoAnWebsite.Models;
using Ext.Net;
using Ext.Net.MVC;
using System.Linq.Dynamic;

namespace DoAnWebsite.Controllers
{
    public class AnswerController : Controller
    {
        private static DoAnWebsite_Entities db = new DoAnWebsite_Entities();
    
        [HttpGet]
        [AccessDeniedAuthorize(Roles = "administrators, users")]
        public ActionResult EditAnswer(int id)
        {
            ViewBag.HideTextBox = false;
            Answer answer = db.Answers.SingleOrDefault(m => m.AskId == id);

            return View(answer);
        }

        [HttpPost]
        [ValidateInput(false)]
        [AccessDeniedAuthorize(Roles = "administrators, users")]
        public ActionResult EditAnswer(Answer model)
        {
            Answer answer1 = db.Answers.Single(c => c.AnswerId == model.AnswerId);
            if (ModelState.IsValid)
            {
                db.Answers.Detach(answer1);
                db.Answers.Attach(model);
                db.ObjectStateManager.ChangeObjectState(model, EntityState.Modified);
                db.SaveChanges();
                X.Msg.Notify("Sucess", "Edit Answer success!").Show();
                return this.FormPanel();
            }
            X.Msg.Notify("Non Sucess", "Edit Answer non success!").Show();
            return this.FormPanel();
        }
    }
}
